<?php
namespace plugins\wx\miniprogram;

require_once  '..\plugins\wx\request.php';

class WXMiniLogin
{

    const JSCODE2SESSION_URL = "https://api.weixin.qq.com/sns/jscode2session?grant_type=authorization_code&";

    public function __construct($appid, $secret)
    {
        $this->appid = $appid;
        $this->secret = $secret;
    }

    public static function __make()
    {
        $merchant = app('merchant');
        return new self($merchant->Wxcode->wxmini_appid, $merchant->Wxcode->wxmini_appsecret);
    }

    public function jscode2session($js_code)
    {
        $url = sprintf("%sappid=%s&secret=%s&js_code=%s", self::JSCODE2SESSION_URL, $this->appid, $this->secret, $js_code);

        $wxResult = get_json($url);
        return $wxResult['openid'];
    }
}